package com.fl.nj.service.impl;

import com.fl.nj.entity.SyncStrategy;
import com.fl.nj.mapper.SyncStrategyMapper;
import com.fl.nj.service.SyncStrategyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 同步策略Service实现类
 * 实现同步策略的业务逻辑
 */
@Service
public class SyncStrategyServiceImpl implements SyncStrategyService {
    @Autowired
    private SyncStrategyMapper syncStrategyMapper;

    @Override
    @Transactional
    public void save(SyncStrategy strategy) {
        // 验证策略名称是否已存在
        SyncStrategy existingStrategy = syncStrategyMapper.getByStrategyName(strategy.getStrategyName());
        if (existingStrategy != null) {
            throw new RuntimeException("策略名称已存在: " + strategy.getStrategyName());
        }
        
        // 验证必填字段
        validateStrategy(strategy);
        
        // 保存策略
        syncStrategyMapper.save(strategy);
    }

    @Override
    @Transactional
    public void update(SyncStrategy strategy) {
        // 验证策略是否存在
        SyncStrategy existingStrategy = syncStrategyMapper.getById(strategy.getId());
        if (existingStrategy == null) {
            throw new RuntimeException("策略不存在");
        }
        
        // 验证策略名称是否已被其他策略使用
        SyncStrategy strategyWithSameName = syncStrategyMapper.getByStrategyName(strategy.getStrategyName());
        if (strategyWithSameName != null && !strategyWithSameName.getId().equals(strategy.getId())) {
            throw new RuntimeException("策略名称已存在: " + strategy.getStrategyName());
        }
        
        // 验证必填字段
        validateStrategy(strategy);
        
        // 更新策略
        syncStrategyMapper.update(strategy);
    }

    @Override
    public SyncStrategy getById(Long id) {
        return syncStrategyMapper.getById(id);
    }

    @Override
    public SyncStrategy getByStrategyName(String strategyName) {
        return syncStrategyMapper.getByStrategyName(strategyName);
    }

    @Override
    public List<SyncStrategy> getAll() {
        return syncStrategyMapper.getAll();
    }

    @Override
    @Transactional
    public void delete(Long id) {
        // 验证策略是否存在
        SyncStrategy strategy = syncStrategyMapper.getById(id);
        if (strategy == null) {
            throw new RuntimeException("策略不存在");
        }
        
        // 删除策略
        syncStrategyMapper.delete(id);
    }

    /**
     * 验证同步策略的必填字段
     * @param strategy 同步策略对象
     */
    private void validateStrategy(SyncStrategy strategy) {
        if (strategy.getStrategyName() == null || strategy.getStrategyName().trim().isEmpty()) {
            throw new RuntimeException("策略名称不能为空");
        }
        if (strategy.getSourceDbConfigId() == null) {
            throw new RuntimeException("源数据库配置不能为空");
        }
        if (strategy.getTargetDbConfigId() == null) {
            throw new RuntimeException("目标数据库配置不能为空");
        }
        if (strategy.getDataFetchRule() == null || strategy.getDataFetchRule().trim().isEmpty()) {
            throw new RuntimeException("数据获取规则不能为空");
        }
        if (strategy.getSyncType() == null || strategy.getSyncType().trim().isEmpty()) {
            throw new RuntimeException("同步类型不能为空");
        }
        if (strategy.getStatus() == null || strategy.getStatus().trim().isEmpty()) {
            throw new RuntimeException("状态不能为空");
        }
        if (strategy.getSyncInterval() != null && strategy.getSyncInterval() < 0) {
            throw new RuntimeException("同步间隔不能小于0");
        }
    }
}